package com.ebc.eservice.service.app.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ebc.eservice.mapper.CardMapper;
import com.ebc.eservice.pojo.EbcCard;
import com.ebc.eservice.pojo.pic.UploadFile;
import com.ebc.eservice.service.app.CardManageService;
import com.ebc.eservice.util.JsonResult;
import com.ebc.eservice.vo.card.updateCardPicVo;
import org.bson.types.Binary;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.Date;

/**
 * @author: Joey
 * @Description:名片管理：曝光统计、客户统计、简介管理
 * 名片下载次数、浏览次数(访客数)、添加通讯录次数、转发次数、留言权限、留言次数、访问停留时长
 * @date:2022/9/23 22:13
 */
@Service
public class CardManageImp implements CardManageService {

    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private CardMapper cardMapper;

    //新增or修改名片个人简介
    @Override
    @Transactional(rollbackFor = Exception.class)
    public JsonResult updatePersonProfile(updateCardPicVo cardPicVo) throws IOException {
        JsonResult result=null;
        result = JsonResult.fail("异常信息");
        //个人简介
        EbcCard card = cardMapper.selectOne(Wrappers.<EbcCard>lambdaQuery()
                .and(wrapper -> wrapper.eq(EbcCard::getId, cardPicVo.getCardId())));
        if(card.getPersonalProfile()==null){//新增
            UploadFile uploadFile = new UploadFile();
            uploadFile.setName(cardPicVo.getFile().getOriginalFilename());
            uploadFile.setCardId(cardPicVo.getCardId());//cardId
            uploadFile.setCreatedTime(new Date());
            uploadFile.setContent(new Binary(cardPicVo.getFile().getBytes()));
            uploadFile.setContentType(cardPicVo.getFile().getContentType());
            uploadFile.setSize(cardPicVo.getFile().getSize());

            UploadFile savedFile = mongoTemplate.save(uploadFile);
            card.setPersonalProfile(cardPicVo.getPersonProfile());
            cardMapper.updateById(card);
            result = JsonResult.success("名片："+savedFile.getCardId()+"添加成功");
            return result;
        }else{//更新
            card.setPersonalProfile(cardPicVo.getPersonProfile());
            cardMapper.updateById(card);
            //更新照片
            UploadFile uploadFile = new UploadFile();
            Query query = new Query(Criteria.where("cardId").is(cardPicVo.getCardId()));
            UploadFile file = mongoTemplate.findOne(query,UploadFile.class);
            if(file!=null){
                file.setContent(new Binary(cardPicVo.getFile().getBytes()));
                file.setName(cardPicVo.getFile().getOriginalFilename());
                file.setUpdateTime(new Date());
                file.setContentType(cardPicVo.getFile().getContentType());
                file.setSize(cardPicVo.getFile().getSize());
                mongoTemplate.save(file);
                result = JsonResult.success(" -名片id:"+cardPicVo.getCardId()+",照片更新成功");
            }else{
                UploadFile uploadFile2 = new UploadFile();
                uploadFile2.setContent(new Binary(cardPicVo.getFile().getBytes()));
                uploadFile2.setName(cardPicVo.getFile().getOriginalFilename());
                uploadFile2.setCreatedTime(new Date());
                uploadFile2.setCardId(cardPicVo.getCardId());
                uploadFile2.setContentType(cardPicVo.getFile().getContentType());
                uploadFile2.setSize(cardPicVo.getFile().getSize());
                mongoTemplate.save(uploadFile2);
                result = JsonResult.success("名片简介id-> "+cardPicVo.getCardId()+",照片新增");
            }
        }
        return result;
    }


}
